//
//  LBLeftTableViewController.m
//  LvoeBeen_Imitation
//
//  Created by Mac on 17/2/19.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LBLeftTableViewController.h"
#import "LBSuperMarketCategories.h"
#import "LBGoodsTableViewCell.h"
static NSUInteger rootViewCount = 0;

@interface LBLeftTableViewController () <LBGoodsTableViewCellDelegate,CAAnimationDelegate>

@end

@implementation LBLeftTableViewController

- (instancetype) initWithSureIsLeft:(BOOL) isLeft {
    if (self = [super init]) {
        self.isLeft = isLeft;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LBGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"CELLRIGHT"];
    self.tableView.rowHeight = _isLeft ? 60 : 100;
    if (!_isLeft) {
        self.tableView.sectionHeaderHeight = SectionHeight;
    }
    
    // Do any additional setup after loading the view.
}

- (void)setCategories:(NSArray<LBSuperMarketCategories *> *)categories {
    _categories = categories;
    [self.tableView reloadData];
}

#pragma mark
#pragma mark - 实现购买商品协议
- (void)addGoodsToShoppingTrolly:(LBGoodsTableViewCell *)goodsTableViewCell imageView:(UIImageView *)iconView {
    [[LBShoppingGoods shared].shoppingGoods addObject:goodsTableViewCell.model];
    rootViewCount = self.view.subviews.count;
    UIImageView *imgView = [[UIImageView alloc] initWithImage:iconView.image];
    imgView.bounds = iconView.bounds;
    [self.view addSubview:imgView];
    // 使用关键帧动画形成抛抛物线添加
    CAKeyframeAnimation *parabola = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    // 准备起点
    CGRect startRect = [iconView convertRect:iconView.bounds toView:self.view];
    CGPoint startPoint = CGPointMake(startRect.origin.x + startRect.size.width / 2, startRect.origin.y + startRect.size.height / 2);
    // 准备中点
    CGPoint middlePoint = CGPointMake(startPoint.x + 40, startPoint.y - 30);
    UIView *basketView = self.tabBarController.tabBar.subviews[1];
    // 准备终点
    CGRect endRect = [basketView convertRect:basketView.bounds toView:self.view];
    CGPoint endPoint = CGPointMake(endRect.origin.x + endRect.size.width / 2, endRect.origin.y + endRect.size.height / 2);
    // 绘制贝塞尔曲线
    [bezierPath moveToPoint:startPoint];
    [bezierPath addQuadCurveToPoint:endPoint controlPoint:middlePoint];
    // 实现关键帧动画
    parabola.path = bezierPath.CGPath;
    parabola.duration = 1.5;
    parabola.removedOnCompletion = YES;
    parabola.fillMode = kCAFillModeForwards;
    parabola.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    parabola.delegate = self;
    parabola.autoreverses = NO;
    [parabola setValue:imgView forKey:@"img"];
    
    // 使用基础动画实现尺寸改变
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"bounds"];
    scale.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, iconView.bounds.size.width / 5, iconView.bounds.size.height / 5)];
    scale.duration = 1.5;
    scale.removedOnCompletion = YES;
    scale.fillMode = kCAFillModeForwards;
    scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//    scale.delegate = self;
    scale.autoreverses = NO;
    
    [imgView.layer addAnimation:parabola forKey:@"buyPath"];
    [imgView.layer addAnimation:scale forKey:@"buySize"];
}

#pragma mark
#pragma mark - 实现动画结束代理
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    UIImageView *imgView = [anim valueForKey:@"img"];
    [imgView removeFromSuperview];
}

#pragma mark
#pragma mark - 实现双 tableView 联动
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 指定代理执行滚动方法
    if (!_isLeft) {
        return;
    }
    
    if ([_delegate respondsToSelector:@selector(tableViewScroll:indexPath:)]){
        [_delegate tableViewScroll:self indexPath:indexPath];
    }
}

#pragma mark
#pragma mark - 实现购买协议

#pragma mark
#pragma mark - 实现滚动代理协议
- (void)tableViewScroll:(LBLeftTableViewController *)leftTableView indexPath:(NSIndexPath *)indexPath {
    // 滚动到指定位置,
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionTop) animated:YES];
    self.tableView.contentInset = UIEdgeInsetsMake(-SectionHeight, 0, 0, 0);
}

#pragma mark
#pragma mark - 重写数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.categories.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _isLeft ? 1 : self.categories[section].goods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = self.isLeft ? @"CELL" : @"CELLRIGHT";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if ([cellID isEqualToString:@"CELL"]) {
        LBSuperMarketCategories *category = self.categories[indexPath.section];
        cell.textLabel.text = category.name;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    } else {
        __weak LBGoodsTableViewCell *goodsCell = (LBGoodsTableViewCell *)cell;
        goodsCell.model = self.categories[indexPath.section].goods[indexPath.row];
        goodsCell.delegate = self;
    }
    
    return cell;
}

#pragma mark
#pragma mark - 设置组头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.isLeft) {
        return nil;
    }
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor whiteColor];
    label.text = self.categories[section].name;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor darkGrayColor];
    [label sizeToFit];
    return label;
}

#pragma mark
#pragma mark - 组头去粘性化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView && !_isLeft) {
        if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= SectionHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        }else if (scrollView.contentOffset.y > SectionHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-SectionHeight, 0, 0, 0);
        }
    }
}

@end

